<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2019/5/14
 * Time: 17:14
 */
//这个是购物车列表php
header("Content-Type:application/json;charset=utf-8");
header("Access-Control-Allow-Origin:*");
include "dbhealper.php";
if($_SERVER["REQUEST_METHOD"]=="GET"){
    $arr=Array("msg"=>"不支持get请求方式","status"=>"0");
    print_r(json_encode($arr));
    return;
}
if(isset($_REQUEST["pro_id"])&&
    isset($_REQUEST["pro_img"])&&
    isset($_REQUEST["pro_name"])&&
    isset($_REQUEST["pro_price"])&&
    isset($_REQUEST["pro_num"])&&
    isset($_REQUEST["u_id"])){
        $PID=$_REQUEST["pro_id"];
        $PIMG=$_REQUEST["pro_img"];
        $PNAME=$_REQUEST["pro_name"];
        $PPRICE=$_REQUEST["pro_price"];
        $PNUM=$_REQUEST["pro_num"];
        $UID=$_REQUEST["u_id"];
//需要判断是否购买过这个商品;购买过 就是num+1;
    //没买过就是把信息插入进去

    //查询的语句
        $sqlcx="SELECT * FROM `carts` WHERE `c_pro_id`='".$PID."' AND `c_u_id`='".$UID."'";
        $resultcx=dbhealper($sqlcx);
    $PZJ=$PPRICE*$PNUM;
               if($resultcx->num_rows>=1){
           //购买过 所以只需要修改数量和总价
            $sqlxg="UPDATE `carts` SET `c_pro_num`='".$PNUM."',`c_pro_zj`='".$PZJ."' WHERE `c_pro_id`='".$PID."' AND `c_u_id`='".$UID."'";
            $resultxg=dbhealper($sqlxg);
            if($resultxg){
                $arr=Array("msg"=>"加入购物车成功X","status"=>"1");
                print_r(json_encode($arr));
            }else{
                $arr=Array("msg"=>"加入购物车失败X","status"=>"-1");
                print_r(json_encode($arr));
            }

       }else{
//           没买过(插入信息)
           $PZJ= $_REQUEST["pro_price"] * $_REQUEST["pro_num"];
        $sqlcr = "INSERT INTO carts (`c_pro_id`,`c_pro_name`,`c_pro_price`,`c_pro_num`,`c_pro_zj`,`c_pro_img`,`c_u_id`)
        VALUES ('" . $_REQUEST["pro_id"] . "','" . $_REQUEST["pro_name"] . "','" . $_REQUEST["pro_price"] . "','" . $_REQUEST["pro_num"] . "','" . $PZJ . "','" . $_REQUEST["pro_img"] . "','" . $_REQUEST["u_id"] . "')";

        $resultcx=dbhealper($sqlcr);
           if($resultcx){
               $arr=Array("msg"=>"加入购物车成功I","status"=>"1");
               print_r(json_encode($arr));
           }else{
               $arr=Array("msg"=>"加入购物车失败I","status"=>"-1");
               print_r(json_encode($arr));
           }
       }

}else{
    $arr=Array("msg"=>"请先登录","status"=>"-10");
    print_r(json_encode($arr));
}